#include "codedelegate.h"

#include <QLineEdit>

QWidget *codeDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QLineEdit *editor =  new  QLineEdit(parent);
    QRegExp regExp( "[a-fA-F0-9]{2}\\s{0,1}[a-fA-F0-9]{2}\\s{0,1}[a-fA-F0-9]{2}\\s{0,1}[a-fA-F0-9]{2}" );
    editor->setValidator( new  QRegExpValidator(regExp, parent));

    return  editor;
}

void codeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QLineEdit *lineEdit =  static_cast <QLineEdit*>(editor);
    lineEdit->setText(text);
}

void codeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QLineEdit *lineEdit =  static_cast <QLineEdit*>(editor);
    QString text = lineEdit->text();
    model->setData(index, text, Qt::EditRole);
}

void codeDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
